package com.zw.pm.module.config.category;

import com.zw.pm.base.BaseController;
import com.zw.pm.common.JsonResult;
import com.zw.pm.common.ValidResult;
import com.zw.pm.gen.entity.Category;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Description: 会议类型 控制层
 *
 * @Author: China.sgl
 * @Date: 2019/11/28 13:48
 */
@Controller
@RequestMapping("/config/category")
public class CategoryController extends BaseController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 会议类型页面
     * @return 页面路径
     */
    @GetMapping
    @RequiresPermissions({"config:category"})
    public String page() {
        return "config/category/page";
    }

    /**
     * 会议类型列表数据
     * @return Json 数据
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"config:category"})
    public JsonResult list(Category category) {
        startPage();
        List<Category> list = categoryService.findCategoryAll(category);
        return jsonResult(list);
    }

    /**
     * 名称唯一验证
     * @param category category
     * @return json
     */
    @ResponseBody
    @PostMapping("/checkUnique")
    @RequiresPermissions({"config:category"})
    public ValidResult checkUnique(Category category) {
        Category p = categoryService.findByCategoryName(category.getName());
        if (category.getId() == null) {
            return ValidResult.result(p);
        }
        if (p == null || category.getId().equals(p.getId())) {
            return ValidResult.SUCCESS;
        }
        return ValidResult.FAIL;
    }

    /**
     * 创建类型
     * @param category category
     * @return json
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"config:category"})
    public JsonResult save(Category category) {
        return jsonResult(categoryService.saveCategory(category));
    }

    /**
     * 根据ID查询
     * @param id id
     * @return Category
     */
    @ResponseBody
    @PostMapping("/id")
    @RequiresPermissions({"config:category"})
    public Category id(Integer id) {
        return categoryService.getCategory(id);
    }

    /**
     * 修改会议类型
     * @param category category
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions({"config:category"})
    public JsonResult update(Category category) {
        return jsonResult(categoryService.updateCategory(category));
    }

    /**
     * 删除
     *
     * @param category category
     * @return json
     */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"config:category"})
    public JsonResult del(Category category) {
        return jsonResult(categoryService.delCategory(category));
    }

}
